import com.sun.org.apache.bcel.internal.generic.LLOAD;

import javax.swing.plaf.SpinnerUI;
import java.util.List;
import java.util.Stack;

/**
 * @User: vitobo
 * @Date: 2024-06-17
 * @Description: 单链表实现
 *
 *
 */

public class MySingleList {

    /**
     * 节点内部类
     */
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;  // 不初始化了, 默认是null

    // 创建一个单链表
    public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        listNode5.next = null;
        this.head = listNode1;
    }

    // 打印链表
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定节点newHead开始打印链表
     * @param newHead
     */
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    // 递归打印单链表
    public void display2(ListNode head){
        if(head == null){
            return;
        }
        if(head.next == null){
            System.out.print(head.val + " ");
            return;
        }
        System.out.println();
    }

    // 将递归改为循环，打印单链表
    public void display3(ListNode head){
        if(head == null){
            return;
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.isEmpty()){
            ListNode ret = stack.pop();
            System.out.print(ret.val + " ");
        }
    }

    // 获得单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    // 查找单链表中是否包含关键字key
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    // 尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(cur == null){
            this.head = node;
        }else{
            while(cur.next != null){
                cur = cur.next;
            }
            // cur已经是尾节点
            cur.next = node;
        }
    }

    // 任意位置插入, 第一个数据节点为0号下标
    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            System.out.println("index位置不合法");
        }
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        // 1.先走index-1步, 找到cur
        while(index-1 != 0){
            cur = cur.next;
            index--;
        }
        // 2.修改指向
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            return;
        }
        // 删除头节点
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }

        ListNode cur = findPrevOfKey(key);
        if(cur == null){
            System.out.println("没有要删除的数字! ");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    // 找到要删除节点的前一个节点
    private ListNode findPrevOfKey(int key){
        ListNode cur = this.head;
        while (cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    // 删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
    }

    // 清空链表
    public void clear(){
        this.head = null;
    }

    // 反转链表
    public ListNode reverseList() {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    // 返回链表的中间节点
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    // 倒数第k个节点
    public ListNode kthToLast(int k) {
        if(k < 0 || head==null){
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        while(k-1 != 0){
            fast = fast.next;
            if(fast == null){
                return null;
            }
            k--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    // 合并两个有序链表
    public ListNode mergeTwoLists(ListNode head1, ListNode head2){
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while(head1 != null && head2 != null){
            if(head1.val < head2.val){
                tmp.next = head1;
                head1 = head1.next;
            }else {
                tmp.next = head2;
                head2 = head2.next;
            }
            tmp = tmp.next;
        }
        if(head1 != null){
            tmp.next = head1;
        }
        if(head2 != null){
            tmp.next = head2;
        }
        return newHead.next;
    }

    // 将小于X的节点排在其余节点之前
    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = this.head;
        // 遍历链表, 当cur为空时,链表遍历完成
        while(cur != null){
            if(cur.val < x){
                // 第一次进行插入节点
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else {
                    // 不是第一次进行插入节点
                    be.next = cur;
                    be = be.next;
                }
            }else{
                if(as == null){
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        // 前一段链表没有数据
        if(bs == null){
            return as;
        }
        be.next = as;
        // 手动置空一次
        if(as != null){
            ae.next = null;
        }
        return bs;
    }

    // 链表的回文结构
    public boolean chkPalindrome() {
        if(head == null){
            return false;
        }
        if(head.next == null){
            return true;
        }

        // 1.找到链表的中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        // 2.翻转中间节点后面的链表
        ListNode cur = slow.next;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        // 3.一个从头开始,一个从尾巴开始,逐节点判断
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            // 处理偶数情况下
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


    // 找到两个链表的第一个公共节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        // 1.求链表的长度
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        while (pl != null){
            lenA++;
            pl = pl.next;
        }

        int lenB = 0;
        while (ps != null){
            lenB++;
            ps = ps.next;
        }

        pl = headA;
        ps = headB;
        int len = lenA - lenB;
        if(len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }

        // pl永远指向最长的链表, ps永远指向最短的链表
        // 2.让最长的链表先走len步,
        while (len != 0){
            pl = pl.next;
            len--;
        }

        // 3.pl和ps 在相同的起始位置
        while (pl != ps){
            pl = pl.next;
            ps = ps.next;
        }

        // 4.走到这里pl和ps相等了, pl和ps都是null了
        if(pl == null){
            return null;
        }
        return pl;
    }

    // 判断链表是否有环
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }

    // 如何找到链表环 的入口点
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        //  2种情况, 一种是 循环条件不满足 一种是遇到break
        if(fast == null || fast.next == null) {
            return null;//代表这个地方 没有环
        }
        slow = head;
        while (slow != fast) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

}
